/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xume.raob.extensions.xstream;

import java.io.OutputStream;
import java.io.Writer;

import org.xume.raob.api.ObjectBuilder;

import com.thoughtworks.xstream.XStream;

/**
 * @author Johan Siebens
 */
public final class XStreamBuilder {

	public static From from(final Object object) {
		return new From() {

			public Object get() {
				return object;
			}

		};
	}
	
	public static From from(final ObjectBuilder<?> builder) {
		return new From() {

			public Object get() {
				return builder.build();
			}

		};
	}

	public static To to(final OutputStream out) {
		return new To() {

			public void execute(Object object, XStream xStream) {
				xStream.toXML(object, out);
			}

		};
	}

	public static To to(final Writer writer) {
		return new To() {

			public void execute(Object object, XStream xStream) {
				xStream.toXML(object, writer);
			}

		};
	}

	public static Using using(final XStream xstream) {
		return new Using() {

			public XStream get() {
				return xstream;
			}

		};
	}

	public static String xml(From from) {
		return XStreamHolder.get().toXML(from.get());
	}

	public static void xml(From from, To to) {
		to.execute(from.get(), XStreamHolder.get());
	}

	public static void xml(From from, To to, Using using) {
		to.execute(from.get(), using.get());
	}

	public static String xml(From from, Using using) {
		return using.get().toXML(from.get());
	}

	private XStreamBuilder() {
	}

	public interface From {

		Object get();

	}

	public interface To {

		void execute(Object object, XStream xStream);

	}

	public interface Using {

		XStream get();

	}

}
